import pandas as pd
import sys
import os

# 添加当前目录到路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from main import ExcelMatcher

def test_complete_functionality():
    """完整测试修复后的功能"""
    
    print("=" * 60)
    print("最终验证：工资匹配功能测试")
    print("=" * 60)
    
    # 创建匹配器实例
    matcher = ExcelMatcher()
    
    # 1. 加载工资表
    print("\n1. 加载工资表...")
    salary_result = matcher.load_salary_file("d:/pycode/工资表.xlsx")
    print(f"工资表加载结果: {salary_result}")
    if salary_result:
        print(f"工资表数据行数: {len(matcher.salary_df)}")
        print(f"工资表列名: {list(matcher.salary_df.columns)}")
    
    # 2. 加载钉钉文件夹
    print("\n2. 加载钉钉文件夹...")
    ding_result = matcher.load_ding_folder("d:/pycode/ding")
    print(f"钉钉文件夹加载结果: {ding_result}")
    if ding_result:
        print(f"钉钉表数据行数: {len(matcher.ding_df)}")
        print(f"钉钉表列名: {list(matcher.ding_df.columns)}")
    
    # 3. 执行匹配
    print("\n3. 执行数据匹配...")
    if salary_result and ding_result:
        match_result = matcher.match_data()
        print(f"匹配执行结果: {match_result}")
        
        if match_result:
            # 检查匹配结果
            matched_count = len(matcher.result_df[matcher.result_df['钉钉中的工资标准'] != '未找到'])
            total_count = len(matcher.result_df)
            
            print(f"\n匹配统计:")
            print(f"  总人数: {total_count}")
            print(f"  成功匹配人数: {matched_count}")
            print(f"  匹配率: {matched_count/total_count*100:.1f}%")
            
            # 显示成功匹配的样本
            matched_samples = matcher.result_df[matcher.result_df['钉钉中的工资标准'] != '未找到'].head(10)
            print(f"\n成功匹配的样本（前10个）:")
            for _, row in matched_samples.iterrows():
                print(f"  {row['姓名']}: 工资表工资={row.get('各项扣款', 'N/A')}, 钉钉工资={row['钉钉中的工资标准']}, 匹配状态={row['是否匹配']}")
            
            # 显示未匹配的样本
            unmatched_samples = matcher.result_df[matcher.result_df['钉钉中的工资标准'] == '未找到'].head(5)
            print(f"\n未匹配的样本（前5个）:")
            for _, row in unmatched_samples.iterrows():
                print(f"  {row['姓名']}: 工资表工资={row.get('各项扣款', 'N/A')}, 钉钉工资=未找到")
        
        # 4. 保存结果
        print("\n4. 保存匹配结果...")
        try:
            output_path = "d:/pycode/匹配结果_验证.xlsx"
            matcher.result_df.to_excel(output_path, index=False)
            print(f"匹配结果已保存到: {output_path}")
        except Exception as e:
            print(f"保存失败: {e}")
    
    print("\n" + "=" * 60)
    print("验证完成！")
    print("=" * 60)

if __name__ == "__main__":
    test_complete_functionality()